/**
 * 
 */
package com.maohi.software.arii.modules;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.maohi.software.arii.jaxb2.Plugin.Jaxb.Xml;
import com.maohi.software.arii.jaxb2.Plugin.Jaxb.Xsd;
import com.maohi.software.arii.utils.Keys;
import com.maohi.software.arii.utils.Providers;

/**
 * @author heifara
 * 
 */
public class JaxBModule extends AbstractModule {
	private static Logger logger = Logger.getLogger(JaxBModule.class.getName());

	@Inject
	private ClassLoader classLoader;

	@Inject
	private Map<Xsd, List<Xml>> jaxb;

	@SuppressWarnings("unchecked")
	@Override
	protected void configure() {
		long iStart = System.currentTimeMillis();

		try {
			for (Entry<Xsd, List<Xml>> iEntry : jaxb.entrySet()) {
				Xsd iXsd = iEntry.getKey();
				List<Xml> iXmls = iEntry.getValue();
				Class<?> iClass = Class.forName(iXsd.getValue(), false, classLoader);
				this.bind(Keys.getListJaxB(iClass, iXsd.getId())).toProvider(Providers.getListJaxB(iXsd, iXmls, classLoader));
			}

		} catch (Exception aException) {
			logger.error(aException.getMessage(), aException);
		} finally {
			logger.debug(String.format("time: %d", System.currentTimeMillis() - iStart));
		}
	}
}
